home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 367_01 / futi14as.zoo / dd.c < prev    next >
C/C++ Source or Header  |  1992-02-22  |  28KB  |  1,036 lines

  1. /* dd -- convert a file while copying it.
  2.    Copyright (C) 1985, 1989, 1990 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Written by Paul Rubin and David MacKenzie. */
  19.  
  20. /* MS-DOS port (c) 1990 by Thorsten Ohl, ohl@gnu.ai.mit.edu
  21.    This port is also distributed under the terms of the
  22.    GNU General Public License as published by the
  23.    Free Software Foundation.
  24.  
  25.    Please note that this file is not identical to the
  26.    original GNU release, you should have received this
  27.    code as patch to the official release.  */
  28.  
  29. #ifdef MSDOS
  30. static char RCS_Id[] =
  31.   "$Header: e:/gnu/fileutil/RCS/dd.c 1.4.0.2 90/09/19 11:17:53 tho Exp $";
  32.  
  33. static char Program_Id[] = "dd";
  34. static char RCS_Revision[] = "$Revision: 1.4.0.2 $";
  35.  
  36. #define VERSION \
  37.   "GNU %s, Version %.*s (compiled %s %s for MS-DOS)\n", Program_Id, \
  38.   (sizeof RCS_Revision - 14), (RCS_Revision + 11), __DATE__, __TIME__
  39.  
  40. #define COPYING \
  41.   "This is free software, distributed under the terms of the\n" \
  42.   "GNU General Public License.  For details, see the file COPYING.\n"
  43. #endif /* MSDOS */
  44.  
  45. /* Options:
  46.  
  47.    Numbers can be followed by a multiplier:
  48.    b=512, k=1024, w=2, xm=number m
  49.  
  50.    if=FILE            Read from FILE instead of stdin.
  51.    of=FILE            Write to FILE instead of stdout; don't
  52.                 truncate FILE.
  53.    ibs=BYTES            Read BYTES bytes at a time.
  54.    obs=BYTES            Write BYTES bytes at a time.
  55.    bs=BYTES            Override ibs and obs.
  56.    cbs=BYTES            Convert BYTES bytes at a time.
  57.    skip=BLOCKS            Skip BLOCKS ibs-sized blocks at
  58.                 start of input.
  59.    seek=BLOCKS            Skip BLOCKS obs-sized blocks at
  60.                 start of output.
  61.    count=BLOCKS            Copy only BLOCKS input blocks.
  62.    conv=CONVERSION[,CONVERSION...]
  63.  
  64.    Conversions:
  65.    ascii            Convert EBCDIC to ASCII.
  66.    ebcdic            Convert ASCII to EBCDIC.
  67.    ibm                Convert ASCII to alternate EBCDIC.
  68.    block            Pad newline-terminated records to size of
  69.                 cbs, replacing newline with trailing spaces.
  70.    unblock            Replace trailing spaces in cbs-sized block
  71.                 with newline.
  72.    lcase            Change uppercase characters to lowercase.
  73.    ucase            Change lowercase characters to uppercase.
  74.    swab                Swap every pair of input bytes.
  75.                 Unlike the Unix dd, this works when an odd
  76.                 number of bytes are read.
  77.    noerror            Continue after read errors.
  78.    sync                Pad every input block to size of ibs with
  79.                 trailing NULs. */
  80.  
  81. #ifdef MSDOS
  82. /*
  83.    im={text,binary}        Input file translation mode (default: text)
  84.    om={text,binary}        Output file translation mode (default: text)
  85.  */
  86. #endif
  87.  
  88. #include <stdio.h>
  89. #include <ctype.h>
  90. #ifdef STDC_HEADERS
  91. #define ISLOWER islower
  92. #define ISUPPER isupper
  93. #else
  94. #define ISLOWER(c) (isascii ((c)) && islower ((c)))
  95. #define ISUPPER(c) (isascii ((c)) && isupper ((c)))
  96. #endif
  97. #include <sys/types.h>
  98. #include <signal.h>
  99. #include "system.h"
  100.  
  101. #ifdef STDC_HEADERS
  102. #include <errno.h>
  103. #include <stdlib.h>
  104. #else
  105. char *malloc ();
  106.  
  107. extern int errno;
  108. #endif
  109.  
  110. #ifndef _POSIX_SOURCE
  111. long lseek ();
  112. #endif
  113.  
  114. #define equal(p, q) (strcmp ((p),(q)) == 0)
  115. #ifndef MSDOS
  116. #define max(a, b) ((a) > (b) ? (a) : (b))
  117. #endif
  118.  
  119. /* Default input and output blocksize. */
  120. #define DEFAULT_BLOCKSIZE 512
  121.  
  122. /* Conversions bit masks. */
  123. #define C_ASCII 01
  124. #define C_EBCDIC 02
  125. #define C_IBM 04
  126. #define C_BLOCK 010
  127. #define C_UNBLOCK 020
  128. #define C_LCASE 040
  129. #define C_UCASE 0100
  130. #define C_SWAB 0200
  131. #define C_NOERROR 0400
  132. #define C_SYNC 01000
  133. /* Use separate input and output buffers, and combine partial input blocks. */
  134. #define C_TWOBUFS 04000
  135.  
  136.  
  137. #ifdef MSDOS
  138.  
  139. #include <io.h>
  140. extern void error (int status, int errnum, char *message, ...);
  141.  
  142. extern void main (int argc, char **argv);
  143. static void copy (void);
  144. static void scanargs (int argc, char **argv);
  145. static int parse_integer (char *str);
  146. static void parse_conversion (char *str);
  147. static void apply_translations (void);
  148. static void translate_charset (unsigned char *new_trans);
  149. static int bit_count (unsigned int i);
  150. static void print_stats (void);
  151. static void quit (int code);
  152. static SIGTYPE interrupt_handler (void);
  153. static char *xmalloc (unsigned short n);
  154. static void usage (char *string, char *arg0, char *arg1);
  155.  
  156. #else /* not MSDOS */
  157.  
  158. char *xmalloc ();
  159. SIGTYPE interrupt_handler ();
  160. int bit_count ();
  161. int parse_integer ();
  162. void apply_translations ();
  163. void copy ();
  164. void error ();
  165. void parse_conversion ();
  166. void print_stats ();
  167. void translate_charset ();
  168. void quit ();
  169. void scanargs ();
  170. void usage ();
  171.  
  172. #endif /* not MSDOS */
  173.  
  174. /* The name this program was run with. */
  175. char *program_name;
  176.  
  177. /* The name of the input file, or NULL for the standard input. */
  178. char *input_file = NULL;
  179.  
  180. /* The input file descriptor. */
  181. int input_fd = 0;
  182.  
  183. /* The name of the output file, or NULL for the standard output. */
  184. char *output_file = NULL;
  185.  
  186. /* The output file descriptor. */
  187. int output_fd = 1;
  188.  
  189. /* The number of bytes in which atomic reads are done. */
  190. #ifdef MSDOS
  191. int input_blocksize = -1;
  192. #else
  193. long input_blocksize = -1;
  194. #endif
  195.  
  196. /* The number of bytes in which atomic writes are done. */
  197. #ifdef MSDOS
  198. int output_blocksize = -1;
  199. #else
  200. long output_blocksize = -1;
  201. #endif
  202.  
  203. /* Conversion buffer size, in bytes.  0 prevents conversions. */
  204. #ifdef MSDOS
  205. int conversion_blocksize = 0;
  206. #else
  207. long conversion_blocksize = 0;
  208. #endif
  209.  
  210. /* Skip this many records of `input_blocksize' bytes before input. */
  211. int skip_records = 0;
  212.  
  213. /* Skip this many records of `output_blocksize' bytes before output. */
  214. #ifdef MSDOS
  215. int seek_record = 0;
  216. #else
  217. long seek_record = 0;
  218. #endif
  219.  
  220. /* Copy only this many records.  <0 means no limit. */
  221. int max_record = -1;
  222.  
  223. /* Bit vector of conversions to apply. */
  224. int conversions_mask = 0;
  225.  
  226. /* Number of partial blocks written. */
  227. unsigned w_partial = 0;
  228.  
  229. /* Number of full blocks written. */
  230. unsigned w_full = 0;
  231.  
  232. /* Number of partial blocks read. */
  233. unsigned r_partial = 0;
  234.  
  235. /* Number of full blocks read. */
  236. unsigned r_full = 0;
  237.  
  238. /* Records truncated by conv=block. */
  239. unsigned r_truncate = 0;
  240.  
  241. #ifdef MSDOS
  242. /* Translation modes */
  243. int input_file_mode = O_TEXT;
  244. int output_file_mode = O_TEXT;
  245. #endif
  246.  
  247. /* Output representation of newline and space characters. */
  248. unsigned char newline_character = '\n';
  249. unsigned char space_character = ' ';
  250.  
  251. struct conversion
  252. {
  253.   char *convname;
  254.   int conversion;
  255. };
  256.  
  257. struct conversion conversions[] =
  258. {
  259.   "ascii", C_ASCII | C_TWOBUFS,    /* EBCDIC to ASCII. */
  260.   "ebcdic", C_EBCDIC | C_TWOBUFS,    /* ASCII to EBCDIC. */
  261.   "ibm", C_IBM | C_TWOBUFS,    /* Slightly different ASCII to EBCDIC. */
  262.   "block", C_BLOCK | C_TWOBUFS,    /* Variable to fixed length records. */
  263.   "unblock", C_UNBLOCK | C_TWOBUFS,    /* Fixed to variable length records. */
  264.   "lcase", C_LCASE | C_TWOBUFS,    /* Translate upper to lower case. */
  265.   "ucase", C_UCASE | C_TWOBUFS,    /* Translate lower to upper case. */
  266.   "swab", C_SWAB | C_TWOBUFS,    /* Swap bytes of input. */
  267.   "noerror", C_NOERROR,        /* Ignore i/o errors. */
  268.   "sync", C_SYNC,        /* Pad input records to ibs with NULs. */
  269.   NULL, 0
  270. };
  271.  
  272. /* Translation table formed by applying successive transformations. */
  273. unsigned char trans_table[256];
  274.  
  275. unsigned char ascii_to_ebcdic[] =
  276. {
  277.   0, 01, 02, 03, 067, 055, 056, 057,
  278.   026, 05, 045, 013, 014, 015, 016, 017,
  279.   020, 021, 022, 023, 074, 075, 062, 046,
  280.   030, 031, 077, 047, 034, 035, 036, 037,
  281.   0100, 0117, 0177, 0173, 0133, 0154, 0120, 0175,
  282.   0115, 0135, 0134, 0116, 0153, 0140, 0113, 0141,
  283.